<?php

namespace App\Http\Controllers\Front;


use App\Http\Requests;
use App\Models\User;
use App\Models\AreaProvince;
use App\Models\AreaCity;
use App\Models\AreaDistrict;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Interfaces\KeywordsService;
use App\Models\Order;
use App\Models\RefundGoods;
use App\Services\Wxpay\Models\Refund;
use Log;
use App\Interfaces\SmsService;
use App\Services\AccountBooks;
use App\Services\CustomsClearance;
use App\Interfaces\OrderService;
use App\Interfaces\CategoryService;
use App\Models\Category;
use App\Services\Rsa;
use App\Services\Wxpay\Wxpay;
use App\Services\Wxpay\Models\UnifiedOrder;
class TestController extends Controller
{

//    public function index(AccountBooks $accountBooks){
//
//        $goods = [
//            'goods_sn'=>'sn001',
//            'tariff_percent'=>'15%',
//            'goods_name'=>'测试商品',
//            'legalInQty'=>'1',
//            'legalOutQty'=>'1',
//            'inventoryCarryInQty'=>'1',
//            'inventoryTurnOutQty'=>'1',
//            'legalRemainQty'=>'1',
//            'realRemainQty'=>'1',
//            'periodStartQty'=>'1'
//        ];
//        $result = $accountBooks->sendPost($goods);
//        dd($result);
//
//    }


    /*public function index(CustomsClearance $customsClearance, OrderService $orderService){
        $order = $orderService->getOrders(59);
        $requestTime = date('Y-m-d H:i:s',time());

//        $result = $customsClearance->sendPost($order,$requestTime);
//        dd($result);

    }*/

    /*public function index(SmsService $sms,User $user)
    {

        $rsa = new Rsa();
        $str = '{"device_id":"865291027243160","client_id":"be4f09b1d8789c176a2a3b5b4a168841"}';
        //dd("gag");
        dd($rsa->encrypt($str));

        $phone = array();
        $command = array();
        $command_phone = array();
        $phone[]='13033751301';
        $phone[]='15890121887';
        //$phone[]='18703644856';
//        $phone[]='18703644851';
//        $phone[]='18703644852';
        $command[] = "hahaha";
        $command[]="hehehe";
        $command[]="hehehe111";
      ;
        if(count($command)>count($phone)){
            //口令多于手机号
            $command = array_slice($command,0,count($phone));
            //$command2 = array_slice($command,-(count($command)-1),count($command)-count($phone));
//            $command2 = array_slice($command,count($phone));
//            dd($command2);
        }
        if(count($phone>count($command))){
            //手机号对于口令
            $phone = array_slice($phone,0,count($command));
        }
        foreach ($phone as $k=>$v) {
            $phone_command[$v]=$command[$k];
        }

        //dd($phone_command);
        $mas = $sms->sendMassMsg($phone_command);
        dd($mas);

//        //dd($phone);
//        //近三个月登录的用户
//        $user = User::where('last_login_time','>',date('Y-m-d H:i:s',strtotime('-3 month')))->get();
//
//        foreach ($user as $k=>$v) {
//            //dd($v->phone);
//            $phone[]=$v->phone;
//        }

        //dd($phone);
        //$message_id =  $sms->sendMassMsg($command_phone);
        //RefundGoods::where('user_id',69)->and('refund_status',1 or 'refund_status',2);
//        $number = RefundGoods::where('user_id',59)->where(function($query){
//            $query->where('refund_status',1)->orWhere(function($query){
//                $query->where('refund_status',2);
//            });
//        })->count();
//        dd($number);





//        $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
//        //检测必填参数
//        if (! $input->isOutTradeNoSet() && ! $input->isTransactionIdSet()) {
//            throw new WxPayException('退款申请接口中，out_trade_no、transaction_id至少填一个！');
//        } elseif (! $input->isOutRefundNoSet()) {
//            throw new WxPayException('退款申请接口中，缺少必填参数out_refund_no！');
//        } elseif (! $input->isTotalFeeSet()) {
//            throw new WxPayException('退款申请接口中，缺少必填参数total_fee！');
//        } elseif (! $input->isRefundFeeSet()) {
//            throw new WxPayException('退款申请接口中，缺少必填参数refund_fee！');
//        } elseif (! $input->isOpUserIdSet()) {
//            throw new WxPayException('退款申请接口中，缺少必填参数op_user_id！');
//        }
//        $input->setAppid(Wxpay::getConfig('appid')); //公众账号ID
//        $input->setMchId(Wxpay::getConfig('mchid')); //商户号
//        $input->setSubMchId(Wxpay::getConfig('sub_mch_id')); //子商户号
//        $input->setNonceStr($this->getNonceStr()); //随机字符串
//        $input->setSign(); //签名
//        $xml = $input->toXml();
//        $start_time_stamp = $this->getMillisecond(); //请求开始时间
//        $response = $this->postXmlCurl($xml, $url, true, $time_out);
//        $result = Results::Init($response);
//        $this->reportCostTime($url, $start_time_stamp, $result); //上报请求花费时间
//        return $result;



//        $alipay->setOutTradeNo($order_sn);
//        $alipay->setTotalFee($pay_price);
//        $alipay->setSubject($subject);
//        $alipay->setBody('');

//        $alipay = app('alipay.web');
//        $alipay->setOutTradeNo($order_sn);
//        $alipay->setTotalFee($pay_price);
//        $alipay->setSubject($subject);
//        $alipay->setBody('');

        $order = Order::find(289);
        //dd($order->order_sn.'^'.$order->pay_price.'^协商退款');
//        $alipay = app('alipay.web');
//        //$alipayrefund->setSellerEmail('1045832553@qq.com');
//        $alipay->setBatchNo(date('Ymd').'001');
//        $alipay->setBatchNum(1);
//        $alipay->setDetailData('2016072521001004920247695827'.'^'.'0.01'.'^协商退款');
//        //dd($alipay->refund());
//        return redirect($alipay->refund());

          dd($alipay->getPayLink());
//        dd($alipay->refund());









//        $wxpay = new Wxpay(config('latrell-wxpay'));
//        dd(config('latrell-wxpay')['mchid']);
//        $refund = $wxpay->refund();
//        $input = new Refund();

//        $appid = 0; //应用ID
//        $mch_id = 0; //商户号
//        $nonce_str = 0;  //随机字符串
//        $sign = 0; //签名
//        $transaction_id=11;
//        $out_trade_no ='G6281001128850d'; //商户订单号
//        $out_refund_no = 'G6281001128850d';  //商户退款单号
//        $total_fee = 100; //总金额
//        $refund_fee = 100; //退款金额
//        $op_user_id = 11; //操作员
//
//        $input->setOutTradeNo($out_trade_no);
//        $input->setOutRefundNo("dasgf");
//        $input->setTotalFee($total_fee);
//        $input->setRefundFee($refund_fee);
//        $input->setOpUserId($op_user_id);
//
//        // $input->SetNotify_url('http://117.158.194.99:8080/wxnotify.php');
//
//        $refund = $refund->refund($input);
//
//        dd($refund);
//
//        $str = '{"page":2}';
//        $enrsa = $rsa->encrypt($str);
//
//        $destr = "DQ1QNnT1FOGkhYzUjcI1BV+Ebsqm7OAwRm7N3KHOIt8A84+XRcd65WuLHvYySZFuRbPgz8x9rG2HDeUP/2x/OkB6OaCe0/1KyhQHdVktM03PjmOqDuup2HM7bM5le1cTkcA4jF6Y60V6ILnxl5pvL0UvuUlJ2P8a9m02Dc9H9xE=";
//        $dersa = $rsa->decrypt($destr);
//        //$categoryInfo = Category::getTreeList();
//    	dd($enrsa);
    }*/

    public function index(Request $request){
        $wxpay = new Wxpay(config('latrell-wxpay'));
        $appid = config('latrell-wxpay')['appid'];
        //https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect
        $url = 'https://open.weixin.qq.com/connect/qrconnect?appid=wx718f583855ba9493&redirect_uri=https%3a%2f%2fwww.afuyigou.com%2fmy%2fcenter&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect';
        header("Location:".$url);
        dd($request->all());

    }

//   public function index(AccountBooks $accountBooks){
//
//        $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
//        $txt = "[";
//
//        $first_level = "\t";
//        $first_level2 = "\t\t";
//        $first_level3 = "\t\t\t";
//        $first_level4 = "\t\t\t\t";
//        $first_level5 = "\t\t\t\t\t";
//        $first_level6 = "\t\t\t\t\t\t";
//
//        $province = AreaProvince::all();
//
//        foreach ($province as $province_key => $province_value) {
//
//
//            /*$province_key==0 ? : ;
//            $txt .= "{\n";*/
//            $txt .= ($province_key==0 ? "" : ",")."\n".$first_level."{\n";
//
//            /*echo '<BR><BR>province_key:'.$province_key;
//            echo '<BR><BR>id:'.$province_value->id;
//            echo '<BR>code:'.$province_value->code;
//            echo '<BR>name:'.$province_value->name;
//            echo '<BR>parent_code:'.$province_value->parent_code;*/
//
//            $txt .= $first_level2.'"id":"'.$province_value->code.'"'.",\n";
//            $txt .= $first_level2.'"name":"'.$province_value->name.'"'.",\n";
//            $txt .= $first_level2.'"child":[';
//
//
//            $province_code = $province_value->code;
//
//            $city = AreaCity::where('parent_code', $province_code)->get();
//
//            foreach ($city as $city_key => $city_value) {
//
//                $txt .= ($city_key==0 ? "" : ",")."\n".$first_level3."{\n";
//
//                $txt .= $first_level4.'"id":"'.$city_value->code.'"'.",\n";
//                $txt .= $first_level4.'"name":"'.$city_value->name.'"'.",\n";
//                $txt .= $first_level4.'"child":[';
//
//                /*echo '<BR><BR>id:'.$city_value->id;
//                echo '<BR>code:'.$city_value->code;
//                echo '<BR>name:'.$city_value->name;
//                echo '<BR>parent_code:'.$city_value->parent_code;*/
//
//                $city_code = $city_value->code;
//
//                $district = AreaDistrict::where('parent_code', $city_code)->get();
//
//                foreach ($district as $district_key => $district_value) {
//
//                    $txt .= ($district_key==0 ? "" : ",")."\n".$first_level5."{\n";
//
//                    $txt .= $first_level6.'"id":"'.$district_value->code.'"'.",\n";
//                    $txt .= $first_level6.'"name":"'.$district_value->name.'"'."\n";
//                    /*$txt .= $first_level6.'"child":[';
//
//                    $txt .= "\n".$first_level6."]\n";*/
//                    $txt .= $first_level5."}";
//                }
//
//                $txt .= "\n".$first_level4."]\n";
//                $txt .= $first_level3."}";
//            }
//
//            $txt .= "\n".$first_level2."]\n";
//            $txt .= $first_level."}";
//        }
//
//
//        $txt .= "\n]";
//
//        fwrite($myfile, $txt);
//        fclose($myfile);
//
//   }


}
